<?php
namespace jy_newcar;
defined('BY_JYA') or exit('error');
/*
	分佣插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商,$isset无条件成为分销商
	*/
	public function checkmember($mid=0,$isexi=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$member['is_agent']){
				if(!$this->setting['g_become']){
					$this->update('member',array('is_agent'=>1,'type'=>!$this->setting['m_become']?2:0),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
					$member['type']=!$this->setting['m_become']?2:0;
					$member['is_agent']=1;
				}elseif(!$this->setting['m_become']){
					$this->update('member',array('is_agent'=>1,'type'=>1),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
					$member['type']=1;
					$member['is_agent']=1;
				}
			}elseif($member['type']==1){
				if(!$this->setting['g_become']){
					$this->update('member',array('type'=>2),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
					$member['type']=2;
				}
			}elseif(!$member['type']){
				if(!$this->setting['m_become']){
					$this->update('member',array('type'=>2),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
					$member['type']=2;
				}
			}
			return $member;
	}
	public function checklogin(){
		if(!$_SESSION['uid']){
			exi('','','jy_newcar/member.index/');
		}
		return true;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_newcar_member',$update,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function check_myshop($mid=0){
		return false;
	}
	/*
		会员注册
	*/
	public function register($member=array(),$mobile=''){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('分销商会员注册失败，缺少会员ID.');
		}
		$ismid=$this->get('member',array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),array('mid'));
		if(!$ismid){
			$member['mobile']=$member['mobile']?:pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'mobile');
			if($this->setting['is_usernum'] && $member['mobile']){
				if($mobile){
					$is_usernum=pdo_count('core_members',array('mobile'=>$member['mobile'],'uniacid'=>$_SESSION['uniacid']));
					if($is_usernum>=$this->setting['is_usernum']){
						$member['nocheck']=1;
					}
				}
			}
			//临时上级
			//$agent_id=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
			//$member['agent_id']=$agent_id;
			$this->insert('member',$member);
		}
		return $member;
	}
	/*
		购车首付款：会员支付购车首付款，成为购车合伙人；购车定金：会员支付购车定金，成为卖车合伙人；
	*/
	public function upmembertype($order=array()){
		if(!is_array($order['api_data'])){
			$order['api_data']=json_decode($order['api_data'],true);
		}
		$buytype=$order['api_data']['buytype'];
		$member=array('mid'=>$order['mid']);
		$this->register($member);
		$member=pdo_get('jy_newcar_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('type','is_agent'));
		if($member['is_agent'] && $buytype==$member['type']){
			return '';
		}elseif($member['is_agent'] && $buytype!=$member['type']){
			pdo_update('jy_newcar_member',array('type'=>2),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		}elseif(!$member['is_agent']){
			pdo_update('jy_newcar_member',array('type'=>$buytype,'is_agent'=>1,'agenttime'=>TIMESTAMP),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return '';
	}
	/*
		申请成为分销商
	*/
	public function apply_agent($mid,$noapply=false,$is_area=0){
		$member=$this->pdo_get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$apply=$this->get('apply',array('is_area'=>$is_area,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($apply && $apply['status']!=2){
			if($apply['status']==1){
				if($is_area){
					$member_update=array(
						'is_area'=>1,
					);
				}else{
					$member_update=array(
						'is_agent'=>1,
						'type'=>$member['is_agent']?2:1,
						'agenttime'=>TIMESTAMP,
					);
				}
				$this->update('member',$member_update,array('mid'=>$apply['mid'],'uniacid'=>$_SESSION['uniacid']));
			}
			return;
		}
		if($apply['status']!=2){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'is_area'=>$is_area,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
		}else{
			$this->update('apply',array('status'=>0),array('is_area'=>$is_area,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$apply_id=$apply['id'];
		}
		return true;
	}
	/*
		审核分销商
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$apply){
			return;
		}
		$member=$this->pdo_get('member',array('mid'=>$apply['mid'],'uniacid'=>$_SESSION['uniacid']));
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			if($apply['is_area']){
				$member_update=array(
					'is_area'=>1,
				);
			}else{
				$member_update=array(
					'is_agent'=>1,
					'type'=>$member['is_agent']?2:1,
					'agenttime'=>TIMESTAMP,
				);
			}
			$this->update('member',$member_update,array('mid'=>$apply['mid'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
	/*
		设置上下级关系
	*/
	public function set_parent($mid=0,$parent_id=0,$ischange=false){
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->get('member',array('mid'=>$mid),array('mid','is_agent','team_mid','level_id','agent_id','createtime'));
		$old_agent_id=$member['agent_id'];
		$isuser=pdo_getcolumn('core_members',array('id'=>$parent_id,'uniacid'=>$_SESSION['uniacid']),'id');
		//如果不存在该会员，已经是分销商或已经有上级了，取消执行。
		if(($old_agent_id || !$isuser || ($member['is_agent'] && $member['createtime']+60<TIMESTAMP)) && !$ischange){
			return true;
		}
		//如果他的上级是我
		$ta_agent_id=$this->getcolumn('member',array('mid'=>$parent_id,'uniacid'=>$_SESSION['uniacid']),'agent_id');
		if($ta_agent_id==$mid){
			if($ischange){
				exi('上级的上级不能是你自己！','error');
			}
			return true;
		}
		//如果上级是自己，上级ID归零
		if($mid==$parent_id){
			if($ischange){
				exi('上级不能是你自己！','error');
			}
			return true;
		}
		$this->update('member',array('agent_id'=>$parent_id,'updatetime'=>time()),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('core_members',array('fromuser'=>$parent_id),array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$agent_id=$old_agent_id;
		$this->update_agent_level($agent_id);
		return true;
	}
	/*
		禁用分销商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
	}
	/*
		恢复分销商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
	}
	/*
		获取会员的各项参数
		
		问题：开启分佣内购时
			推广订单如何算？
			下线如何算？
			
			计算下线时，分佣级数在设置的级数基础上-1，其他不变
			计算推广订单时，在上面的基础上+自己的订单
	*/
	public function get_member($mid){
		$res=array();
		//获取一级下线数量
		$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid),array('mid','is_agent'),'mid');		
		if(!$members){
			$res=array(
				'member_num'=>'0',
				'agent_num'=>'0',
				'order_price'=>'0',
				'order_num'=>'0',
				'myorder_price'=>'0',
				'order_price_next'=>'0',
				'order_num_next'=>'0',
				'member_num_next'=>'0',
				'agent_num_next'=>'0',
			);
			return $res;
		}
		//获取直属下级分销商数量
		$res['agent_num_next']=$this->count('member',array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'agent_id'=>$mid));
		$res['member_num_next']=count($members);
		$member_ids=array_keys($members);
		$res['order_num_next']=$this->count('order',array('mid'=>$member_ids));
		$res['order_price_next']=$this->sum('order',array('mid'=>$member_ids),'order_price');
		//获取下线总数，向下遍历系统设置的指定分佣层数
		$commission_level=intval($this->setting['level']);
		if($this->setting['self_commission']){
			//开启分佣内购
			$commission_level=$commission_level-1;
		}
		if($commission_level>1){
			$next_ids=$member_ids;
			for($i=1;$i<$commission_level;$i++){
				$next_members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$next_ids),array('mid','is_agent'),'mid');
				if(!$next_members){
					break;
				}
				$members=array_merge($next_members,$members);
				$next_ids=array_keys($next_members);
				$member_ids=array_merge($next_ids,$member_ids);
			}
		}
		$res['member_num']=count($member_ids);
		$res['order_num']=$this->count('order',array('mid'=>$member_ids));
		$res['order_price']=$this->sum('order',array('mid'=>$member_ids),'order_price');
		$res['myorder_price']=$this->sum('order',array('mid'=>$mid),'myorder_price');
		$res['agent_num']=0;
		foreach($members as $member){
			if($member['is_agent']){
				$res['agent_num']++;
			}
		}
		if($this->setting['self_commission']){
			//开启分佣内购，此处加上用户自己的订单作为推广订单的一部分
			$self_order_num=$this->count('order',array('mid'=>$mid));
			$self_order_money=$this->sum('order',array('mid'=>$mid),'order_price');
			$res['order_num_next']=$self_order_num;
			$res['order_price_next']=$self_order_money;
			$res['order_num']+=$self_order_num;
			$res['order_price']+=$self_order_money;
		}
		return $res;
	}
	
	public function getteamids($teammid=0,$level=0){
		$teamids=$this->getsysmids($teammid,$level);
		return $teamids;
		$teammid=$teammid?:$_SESSION['uid'];
		$newmembers=array();
		$newteammids=$teammid;
		$level=$level?:58;
		for($i=1;$i<$level;$i++){
			$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$newteammids),array('mid'),'mid');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=array();
		}
		return $teamids;
	}
	public function getsysmids($teammid=0,$level=0){
		$teammid=$teammid?:$_SESSION['uid'];
		$newmembers=array();
		$newteammids=$teammid;
		$level=$level?:58;
		for($i=1;$i<$level;$i++){
			$members=pdo_getall('core_members',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$newteammids),array('id'),'id');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=array();
		}
		return $teamids;
	}
	public function getdownminfee($teammid=0){
		$teammid=$teammid?:$_SESSION['uid'];
		$members=pdo_getall('core_members',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$teammid),array('id'));
		$maxfee=0;
		$allfee=0;
		foreach($members as $mv){
			$downmids=$this->getteamids($mv['id']);
			$downmids[]=$mv['id'];
			$order_price=pdo_getsum('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$downmids),'price');
			if($order_price>$maxfee || !$maxfee){
				$maxfee=$order_price;
			}
			$allfee +=$order_price;
		}
		return $allfee-$maxfee;
	}
	public function getdownminfee2($teammid=0){
		$teammid=$teammid?:$_SESSION['uid'];
		$members=pdo_getall('core_members',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$teammid),array('id'));
		$minfee=0;
		foreach($members as $mv){
			$downmids=$this->getteamids($mv['id']);
			$downmids[]=$mv['id'];
			$order_price=pdo_getsum('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$downmids),'price');
			if($order_price<$minfee || !$minfee){
				$minfee=$order_price;
			}
		}
		return $minfee;
	}
}